package com.gujiangbo.redemo;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

public class JedisFactory {

    /**
     * jedis 直接接
     *
     * @return
     */
    public static Jedis getJedis() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(0);
        return jedis;
    }

    /**
     * JedisPool 连接池
     *
     * @return
     */
    public static JedisPool getJedisPool() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        //设置最大连接数为默认值的5倍
        poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 5);
        //设置最大空闲连接数为默认值的5倍
        poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 5);
        //设置开启JMX功能
        poolConfig.setJmxEnabled(true);
        //设置连接池没有连接后客户端的最大等待时间(单位为毫秒)
        // poolConfig.setMaxWaitMillis(300000000);
        JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6380);
        System.out.println(jedisPool);
        return jedisPool;
    }

    public static Pipeline getPipeLine() {
        return getJedis().pipelined();
    }
}
